package com.wie.service.impl;

import com.wie.mapper.CategoryMapper;
import com.wie.pojo.Category;
import com.wie.service.CategoryService;
import com.wie.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增文章分类
     * @param category
     */
    public void add(Category category) {
        Map<String, Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        category.setCreateUser(userId);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.add(category);
    }

    /**
     * 查询文章分类列表
     * @return
     */
    public List<Category> list() {
        Map<String, Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        List<Category> categoryList = categoryMapper.list(userId);
        return categoryList;
    }

    /**
     * 根据文章id获取文章分类详情
     * @param categoryId
     * @return
     */
    public Category findCategoryByCategoryId(Integer categoryId) {
        Category category = categoryMapper.findCategoryByCategoryId(categoryId);
        return category;
    }

    /**
     * 更新文章分类
     * @param category
     */
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    /**
     * 删除文章分类
     * @param categoryId
     */
    public void delete(Integer categoryId) {
        categoryMapper.delete(categoryId);
    }
}
